﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace OGS.Utility
{
    public class SiteImage
    {
        public string Name { get; set;}
        public string Path { get; set;}
    }

    public class SiteImageExplorer
    {
        /// <summary>
        /// Get All Images
        /// </summary>
        /// <returns></returns>
        public List<SiteImage> Search(string Name)
        {
            string FolderPath = HttpContext.Current.Server.MapPath(Settings.SiteImagesFolderPath);
            List<SiteImage> SiteImages = new List<SiteImage>();

            string[] extensions = { "jpg", "png", "gif" };

            string[] filePaths = Directory.GetFiles(FolderPath, "*.*").Where(f => extensions.Contains(f.Split('.').Last().ToLower())).ToArray();

            //search in array
            List<string> results = filePaths.OrderBy(f => f).ToList();
            if (Name != string.Empty)
            {
                results = results.Where(r => r.IndexOf(Name, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
            }


            if (results != null)
            {
                foreach (string filepath in results)
                {
                    string FileName = Path.GetFileName(filepath);
                    SiteImages.Add(new SiteImage
                    {
                        Name = FileName,
                        Path = string.Concat(Settings.SiteImagesFolderPath + FileName).Replace("~", "")
                    });
                }
            }

            return SiteImages;
        }
    }

   
}
